package com.mrd.gtimp.business.action;

import com.mrd.constant.MsgConstants;
import com.mrd.exception.QueryException;
import com.mrd.exception.SaveException;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.config.web.json.annotation.JField;
import com.mrd.framework.config.web.json.annotation.Json;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.gtimp.business.entity.DeclarationPort;
import com.mrd.gtimp.business.service.DeclarationPortBiz;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by lenovo on 2017/5/22.
 */
@Controller
@RequestMapping("/declarationPort")
public class DeclarationPortAction {

    /**
     * 日志
     */
    private static final Logger logger = Logger.getLogger(DeclarationPortAction.class);

    @Resource
    private DeclarationPortBiz declarationPortBiz;

    /**
     * 申报口岸列表页面
     * @return
     */
    @RequestMapping("/main")
    public String main(){
        return "gtimp3/declarationPort/main";
    }

    /**
     * 申报口岸详细页面
     * @return
     */
    @RequestMapping("/detail")
    public String detail(Model model ,String id){
        if(StringUtils.isNotEmpty(id)){
            model.addAttribute("id",id);
            model.addAttribute("addState","1");
        }
        return "gtimp3/declarationPort/detail";
    }

    /**
     * 查询申报口岸
     * @param jqgrid
     * @return
     */
    @RequestMapping("/findList")
    @ResponseBody
    @Json({@JField("id"),@JField("portCode"),@JField("portName"),@JField("portInitials"),@JField("portSort"),@JField("hasSO")})
    public Page<DeclarationPort> findList(Jqgrid jqgrid){
        try {
            return declarationPortBiz.findByJqgrid(jqgrid);
        } catch (QueryException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据ID查找申报口岸
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    @ResponseBody
    public DeclarationPort findById(@RequestParam() String id){
        try {
            return declarationPortBiz.findEntityById(id);
        } catch (QueryException e) {
            e.printStackTrace();
        }
        return null;
    }



    /**
     * 申报口岸新增或修改
     * @param declarationPort
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Result saveOrUpdate(DeclarationPort declarationPort){
        try {
            declarationPortBiz.saveOrupdateEntity(declarationPort);
            return Result.newSuccessInstance(MsgConstants.SAVE_SUECCES);
        } catch (SaveException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.newFailInstance(MsgConstants.SAVE_FAIL);
    }

}
